package com.jxt.myjxtmanager.views;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.utils.SystemInfo;

/**
 * 检查更新 dialog
 * 
 * @author lkk
 * 
 */
public class DialogView extends Dialog implements
		View.OnClickListener {

	/** Dialog style */
	static int theme = R.style.MyDialog;

	private TextView dialogContent;
	private Button dialogCancel;
	private Button dialogSure;
	private TextView dialogTitle;
	OnDialogClickListener instance;
	OnDialogCancelClickListener instanceCancel;
	/** 是否是一个按键 */
	private boolean isSingleButton = false;
	
	private String title;
	private String rightMenu;
	private String content;
	
	SpannableString mSpannableString;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.widget_dailog_view);
		DisplayMetrics display = SystemInfo.getDeviceDisplayMetrics(this
				.getContext());
		android.view.WindowManager.LayoutParams lp = this.getWindow()
				.getAttributes();
		lp.width = display.widthPixels - display.widthPixels / 5;
		lp.height = LayoutParams.WRAP_CONTENT;
		this.getWindow().setAttributes(lp);

		dialogTitle = (TextView) findViewById(R.id.dialog_title);
		dialogContent = (TextView) findViewById(R.id.dialog_content);
		dialogSure = (Button) findViewById(R.id.dialog_sure);
		dialogSure.setOnClickListener(this);
		dialogCancel = (Button) findViewById(R.id.dialog_cancel);
		if (isSingleButton) {
			dialogSure.setBackgroundResource(R.drawable.bg_circle_dialog);
			TextView line = (TextView) findViewById(R.id.dialog_center_line);
			line.setVisibility(View.GONE);
			dialogCancel.setVisibility(View.GONE);
		} else {
			dialogCancel.setOnClickListener(this);
		}
		setCanceledOnTouchOutside(false);
		
		
		dialogTitle.setText(title);
		if(TextUtils.isEmpty(content)){
			dialogContent.setText(mSpannableString);
			dialogContent.setMovementMethod(LinkMovementMethod.getInstance());
		}else{
			dialogContent.setText(content);
		}
		if(TextUtils.isEmpty(rightMenu)){
			dialogSure.setText(dialogSure.getContext().getString(R.string.sure));
		}else{
			dialogSure.setText(rightMenu);
		}
	}

	/**
	 * Ĭ默认
	 * 
	 * @param context-
	 */
	public DialogView(Context context) {
		super(context, theme);
	}

	/**
	 * @param context
	 * @param isSingleButton
	 *            是否是一个按键默认两个
	 */
	public DialogView(Context context, boolean isSingleButton) {
		super(context, theme);
		this.isSingleButton = isSingleButton;
	}

	/**
	 * �Զ���Dialog
	 * 
	 * @param context
	 * @param title
	 *            ����
	 * @param content
	 *            dialog ��ʾ����
	 * @param onClick
	 *            ȷ����������ص��¼�
	 */
	public void showDialog(String title, String content,
			OnDialogClickListener onClick) {
		instance = onClick;
		this.title = title;
		this.content = content;
		show();
	}

	/**
	 * �Զ���Dialog
	 * 
	 * @param context
	 * @param title
	 *            ����
	 * @param content
	 *            dialog ��ʾ����
	 * @param onClick
	 *            ȷ����������ص��¼�
	 */
	public void showDialog(String title, String content,
			OnDialogClickListener onClick,
			OnDialogCancelClickListener onCancelClick) {
		instance = onClick;
		instanceCancel = onCancelClick;
		this.title = title;
		this.content = content;
		show();
	}
	
	public void setShowDialog(String title, SpannableString content,
			OnDialogClickListener onClick,
			OnDialogCancelClickListener onCancelClick) {
		instance = onClick;
		instanceCancel = onCancelClick;
		this.title = title;
		this.content = "";
		this.mSpannableString = content;
	}

	/**
	 * �Զ���Dialog
	 * 
	 * @param context
	 * @param title
	 *            ����
	 * @param �����ұ߰����ı�
	 * @param content
	 *            dialog ��ʾ����
	 * @param onClick
	 *            ȷ����������ص��¼�
	 */
	public void showDialog(String title, String content, String rightMenu,
			OnDialogClickListener onClick) {
		instance = onClick;
		this.title = title;
		this.rightMenu = rightMenu;
		this.content = content;
		show();
	}

	/**
	 * 
	 * @param title
	 *            标题
	 * @param content
	 *            文本
	 * @param RightMenu
	 *            自定义 右边按键文本
	 * @param onClick
	 *            确定按键监听
	 * @param onCancelClick
	 *            取消按键监听
	 */
	public void showDialog(String title, String content, String rightMenu,
			OnDialogClickListener onClick,
			OnDialogCancelClickListener onCancelClick) {
		instance = onClick;
		instanceCancel = onCancelClick;
		this.title = title;
		this.rightMenu = rightMenu;
		this.content = content;
		show();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.dialog_sure:
			instance.onClick(this);
			break;
		case R.id.dialog_cancel:
			if (instanceCancel != null) {
				instanceCancel.onClick(this);
			}
			dismiss();
			break;

		default:
			break;
		}

	}

	

	/**
	 * Dialog ȷ����ť����ص��������
	 * 
	 */
	public interface OnDialogClickListener {
		void onClick(Dialog dialog);
	}

	/**
	 * Dialog ȡ����ť����ص��������
	 * 
	 */
	public interface OnDialogCancelClickListener {
		void onClick(Dialog dialog);
	}
	// public void setTitleColor(int color){
	// dialogTitle.setTextColor(color);
	// }
}
